package com.csii.config.web;

import com.csii.adhoc.exception.ActStartException;
import com.csii.adhoc.exception.ActSubmitException;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 全局异常处理类
 * @author: panChuanJiang
 * @create: 2021-01-20 10:15
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 一般的参数绑定时候抛出的异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public APIResult<String> handleBindException(MethodArgumentNotValidException ex) {
        log.error("参数校验异常{}", ex.getMessage());
        List<String> defaultMsg = ex.getBindingResult().getAllErrors()
                .stream()
                .map(ObjectError::getDefaultMessage)
                .collect(Collectors.toList());
        return APIResult.failResult("参数校验异常", defaultMsg.get(0));
    }

    /**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = ParamValidityException.class)
    @ResponseBody
    public APIResult<String> bizExceptionHandler(HttpServletRequest req, ParamValidityException e) {
        log.error("发生业务异常！原因是：{}", e.getMessage());
        return APIResult.failResult(e.getMessage(), "参数校验异常");
    }

    /**
     * 处理自定义审批流启动异常
     * @param e
     * @author: zhanghao
     * @return
     */
    @ExceptionHandler(value = ActStartException.class)
    @ResponseBody
    public APIResult<String> ActStartExceptionHandler(ActStartException e) {
        log.error("发生业务异常！原因是：{}", e.getMessage());
        return APIResult.failResult("审批流启动异常", e.getMessage());
    }

    /**
     * 处理自定义审批流程提交异常
     * @Author: zhanghao
     * @param e
     * @return
     */
    @ExceptionHandler(value = ActSubmitException.class)
    @ResponseBody
    public APIResult<String> ActSubmitExceptionHandler(ActSubmitException e){
        log.error("发生业务异常!原因是: {}", e.getMessage());
        return APIResult.failResult("审批流提交异常", e.getMessage());
    }
    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public APIResult<String> exceptionHandler(HttpServletRequest req, NullPointerException e) {
        log.error("发生空指针异常！原因是:", e);
        return APIResult.failResult(e.getMessage(), "空指针异常");
    }


    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public APIResult<String> exceptionHandler(HttpServletRequest req, Exception e) {
        log.error("未知异常！原因是:", e);
        return APIResult.failResult(e.getMessage(), "系统异常");
    }
}
